from typing import Callable

from fastapi import FastAPI
from loguru import logger

from app.db.redis import redis_init, r
from app.db.search import es_init, es


def create_start_app_handler(app: FastAPI) -> Callable:
    async def start_app() -> None:
        # 补充APP启动时需要的动作，例如：链接数据库
        pass

    return start_app


def create_stop_app_handler(app: FastAPI) -> Callable:
    @logger.catch
    async def stop_app() -> None:
        # 补充APP关闭时需要的动作：例如关闭数据库
        if es:
            await es.close()
        # if r:
        #     await r.close()
        pass

    return stop_app

